package io.xxx.omni.oss.domain

import com.google.gson.JsonObject
import io.xxx.omni.oss.common.gson
import java.time.LocalDateTime

data class Authorization(
    val id: Int,
    val pid: String,
    val sid: String?,
    val data: String,
    val enabled: Boolean,
    val created: LocalDateTime,
    val modified: LocalDateTime,
) {
    operator fun get(key: String): String = dataJson.get(key).asString

    private val dataJson by lazy {
        gson.fromJson(data, JsonObject::class.java)
    }

    override fun equals(other: Any?): Boolean {
        if (this === other) return true
        if (javaClass != other?.javaClass) return false

        other as Authorization

        if (id != other.id) return false
        if (modified != other.modified) return false

        return true
    }

    override fun hashCode(): Int {
        var result = id.hashCode()
        result = 31 * result + modified.hashCode()
        return result
    }

    operator fun compareTo(other: Authorization): Int {
        return modified.compareTo(other.modified)
    }
}